package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


import atg.taglib.json.util.JSONObject;
import benny_driving_ser.ObjectBean.PayCodeBean;
import benny_driving_ser.ObjectBean.TaximeterDataBean;
import benny_driving_ser.dao.PayCodeDao;
import benny_driving_ser.dao.TaximeterDataSubmitDao;
import benny_driving_ser.dao.impl.PayCodeDaoImpl;
import benny_driving_ser.dao.impl.TaximeterDataSubmitDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.PayCodeUtil;
import benny_driving_ser.util.SessionUtil;

public class TaximeterDataSubmitAction extends BaseAction {

	private TaximeterDataSubmitDao dao;

	private String period;
	private String orderid;
	private String total;
	private String startmil;
	private String endmil;
	private String sm;
	private String sop;
	private String sowt;
	private String sowp;

	private String payCode;
	private TaximeterDataSubmitDao dao_submit;
	private JSONObject json;
	private PayCodeDao dao_code;
	private String dri_id;

	private TaximeterDataBean taximeterData;
	private PrintWriter out;

	private long currentTime;

	private int stateNum;

	public String taximeterStart() {
		dao = new TaximeterDataSubmitDaoImpl();
		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			if (sad != null) {
				int dri_id = Integer.parseInt(sad.getValue());
				currentTime = dao.taximeterDataSubmit_start(dri_id);
				if (currentTime != 0) {
					out.print(currentTime);
					out.flush();
					out.close();
					return "success";
				} else {
					out.print(1);
					out.flush();
					out.close();
					return "fail";
				}
			} else {
				out.print(-1);
				out.flush();
				out.close();
				return "fail";
			}
		} catch (IOException e) {
			e.printStackTrace();
			out.print(2);
			out.flush();
			out.close();
			return "fail";
		}
	}

	public String submitData() {
		int payCost;
		List<PayCodeBean> list = null;
		dao_code = new PayCodeDaoImpl();
		dao = new TaximeterDataSubmitDaoImpl();
		json = new JSONObject();

		SessionAttributeDriId sad = SessionUtil.getInstance()
				.verifySessionIdForDri(request);
		try {
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			if (sad != null) {
				String dri_id = sad.getValue();
				if(payCode==null || payCode.equals("")){
					taximeterData = new TaximeterDataBean(period,
							orderid, total, sm, sop, sowt, sowp,
							startmil, endmil, dri_id);
					stateNum = dao.taximeterDataSubmit(taximeterData,0,"");
					if (stateNum == 0) {
						json.put("state", "0");
						json.put("price", "0");
						out.print(json.toString());
						out.flush();
						out.close();
						return "success";
					}else{
						json.put("state", "6");
						json.put("price", "0");
						out.print(json.toString());
						out.flush();
						out.close();
						return "fail";
					}
					
				}else{
					if(dao_code.checkPayCode(payCode.trim()) == 1){ 
						if ((list = dao_code.queryPayCode(payCode)) != null) {
							if (Integer.parseInt(period) == Integer.parseInt(list
									.get(0).getDayOrNight())|| Integer.parseInt(list.get(0).getDayOrNight())==2) {//日卡,夜卡
								taximeterData = new TaximeterDataBean(period,
										orderid, total, sm, sop, sowt, sowp,
										startmil, endmil, dri_id);
								payCost = PayCodeUtil.calcCodeCost(list,
										taximeterData) == 0 ? 0 : PayCodeUtil
										.calcCodeCost(list, taximeterData);
								if (stateNum == 0) {
										stateNum = dao
												.taximeterDataSubmit(taximeterData,payCost,payCode);
										if(stateNum==0){
											json.put("state", "0");
											json.put("price", payCost + "");
											out.print(json.toString());
											out.flush();
											out.close();
											System.out.println("支付成功..支付码抵扣" + payCost+ "元!");
											return "success";
										}else{
											json.put("state", "6");
											json.put("price", payCost + "");
											out.print(json.toString());
											out.flush();
											out.close();
											return "fail";
										}
								}
							} else {
								json.put("state", "2");
								json.put("price", "0");
								out.print(json.toString());
								out.flush();
								out.close();
								return "fail";
							}
							json.put("state", "6");
							json.put("price", "0");
							out.print(json);
							out.flush();
							out.close();
							return "fail";
						} else {
							json.put("state", "3");
							json.put("price", "0");
							out.print(json.toString());
							out.flush();
							out.close();
							return "fail";
						}
					}else{
						json.put("state", "1");
						json.put("price", "0");
						out.print(json.toString());
						out.flush();
						out.close();
						return "fail";
					}
				}
			} else {
				json.put("state", "-1");
				json.put("price", "0");
				out.print(json.toString());
				out.flush();
				out.close();
				return "fail";
			}
		} catch (IOException e) {
			e.printStackTrace();
			try {
				json.put("state", "6");
				json.put("price", "0");
			} catch (atg.taglib.json.util.JSONException e1) {
				e1.printStackTrace();
			}
			out.print(json.toString());
			out.flush();
			out.close();
			return "fail";
		} catch (atg.taglib.json.util.JSONException e) {
			e.printStackTrace();
			try {
				json.put("state", "7");
				json.put("price", "0");
			} catch (atg.taglib.json.util.JSONException e1) {
				e1.printStackTrace();
			}
			out.print(json.toString());
			out.flush();
			out.close();
			return "fail";
		}
	}

	public int getStateNum() {
		return stateNum;
	}

	public void setStateNum(int stateNum) {
		this.stateNum = stateNum;
	}

	public TaximeterDataSubmitDao getDao() {
		return dao;
	}

	public void setDao(TaximeterDataSubmitDao dao) {
		this.dao = dao;
	}

	public String getPeriod() {
		return period;
	}

	public void setPeriod(String period) {
		this.period = period;
	}

	public String getOrderid() {
		return orderid;
	}

	public void setOrderid(String orderid) {
		this.orderid = orderid;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getStartmil() {
		return startmil;
	}

	public void setStartmil(String startmil) {
		this.startmil = startmil;
	}

	public String getEndmil() {
		return endmil;
	}

	public void setEndmil(String endmil) {
		this.endmil = endmil;
	}

	public String getSm() {
		return sm;
	}

	public void setSm(String sm) {
		this.sm = sm;
	}

	public String getSop() {
		return sop;
	}

	public void setSop(String sop) {
		this.sop = sop;
	}

	public String getSowt() {
		return sowt;
	}

	public void setSowt(String sowt) {
		this.sowt = sowt;
	}

	public String getSowp() {
		return sowp;
	}

	public void setSowp(String sowp) {
		this.sowp = sowp;
	}

	public TaximeterDataBean getTaximeterData() {
		return taximeterData;
	}

	public void setTaximeterData(TaximeterDataBean taximeterData) {
		this.taximeterData = taximeterData;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	public String getPayCode() {
		return payCode;
	}

	public void setPayCode(String payCode) {
		this.payCode = payCode;
	}

	public TaximeterDataSubmitDao getDao_submit() {
		return dao_submit;
	}

	public void setDao_submit(TaximeterDataSubmitDao dao_submit) {
		this.dao_submit = dao_submit;
	}

	public JSONObject getJson() {
		return json;
	}

	public void setJson(JSONObject json) {
		this.json = json;
	}

	public PayCodeDao getDao_code() {
		return dao_code;
	}

	public void setDao_code(PayCodeDao dao_code) {
		this.dao_code = dao_code;
	}

	public String getDri_id() {
		return dri_id;
	}

	public void setDri_id(String dri_id) {
		this.dri_id = dri_id;
	}

}
